package com.hqyj.finance.server.users.financeusers.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.finance.server.users.financeusers.entity.Users;
import com.hqyj.finance.server.users.financeusers.entity.UsersRoles;
import com.hqyj.finance.server.users.financeusers.mapper.UsersMapper;
import com.hqyj.finance.server.users.financeusers.service.UsersRolesService;
import com.hqyj.finance.server.users.financeusers.service.UsersService;
import com.hqyj.finance.server.users.financeusers.utils.MyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private UsersMapper usersMapper;
    @Resource
    private UsersRolesService usersRolesService;


    @Override
    public List<Users> queryAllUsers() {
        return usersMapper.queryAllUsers();
    }

    @Override
    public Users login(String identify, String password) throws Exception {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        //判断一下你传进来的identify是手机还是邮箱；
        if (MyUtils.isMobileNumber(identify)) {
            queryWrapper.eq("mobile", identify);
        } else if (MyUtils.isEmail(identify)) {
            queryWrapper.eq("email", identify);
        } else {
            queryWrapper.eq("username", identify);
        }

        Users loginSuccessUser = super.getOne(queryWrapper);
        if (loginSuccessUser != null) {

            if (MyUtils.checkPassword(password, loginSuccessUser.getPassword())) {
                return loginSuccessUser;
            }
            return null;
        }
        return null;
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean reg(Users u) throws Exception {
        try {
            //随机生成一个用户名
            u.setUsername(IdUtil.simpleUUID()); //保存随机生成的用户名
            u.setPassword(MyUtils.encodePassword(u.getPassword()));
            u.setGender("男");
            u.setBirthday("2000-01-01");
            //设置默认的头像
            u.setAvatar("https://img.simoniu.com/vue-color-avatar11.png");

            if(super.save(u)){
                //新注册的用户名默认分配普通用户权限
                UsersRoles usersRoles = new UsersRoles();
                usersRoles.setUid(u.getUid());
                usersRoles.setRid(2);
                return usersRolesService.save(usersRoles);
            }else{
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public boolean checkIdentifyIsReg(String identify) throws Exception {

        QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
        queryWrapper.eq("mobile", identify).or().eq("email", identify);
        return super.list(queryWrapper).size() > 0;

    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean updateUsersProfile(Users u) throws Exception {
        try {
            return super.updateById(u);
        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public Users queryUserProfileByUid(Integer uid) throws Exception {

        //return super.getById(uid);
        return usersMapper.queryUserByUidWithoutDeleteFlag(uid);
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public boolean updateUserDeleteFlagByUid(Integer uid, Integer flag) throws Exception {
        try{
            usersMapper.updateUserDeleteFlagByUid(flag,uid);
            return true;
        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }
}
